package com.rongc.client.freight.business.waybill.view.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.reflect.TypeToken;
import com.rongc.client.core.gson.GsonHelper;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.pay.DoAlipay;
import com.rongc.client.core.pay.alipay.util.OrderInfoUtil2_0;
import com.rongc.client.core.pay.wxpay.WxpayHelper;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.KeyboardUtil;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.model.BaseDic;
import com.rongc.client.freight.base.model.OrderDic;
import com.rongc.client.freight.base.request.result.NullResult;
import com.rongc.client.freight.base.view.activity.HostActivity;
import com.rongc.client.freight.base.view.activity.MapGdActivity;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;
import com.rongc.client.freight.business.carrier.model.ConveyanceDic;
import com.rongc.client.freight.business.mine.view.activity.PasswordPayActivity;
import com.rongc.client.freight.business.mine.view.adapter.RecyclerKeyboardAdapter;
import com.rongc.client.freight.business.mine.view.popup.MinePopup;
import com.rongc.client.freight.business.supply.model.SupplyDic;
import com.rongc.client.freight.business.supply.view.fragment.SupplyFragment;
import com.rongc.client.freight.business.supply.view.fragment.UserFragment;
import com.rongc.client.freight.business.supply.view.popup.SupplyPopup;
import com.rongc.client.freight.business.waybill.model.LogisticsBean;
import com.rongc.client.freight.business.waybill.model.WayBillDetailBean;
import com.rongc.client.freight.business.waybill.request.api.WaybillCancelApi;
import com.rongc.client.freight.business.waybill.request.api.WaybillDetailApi;
import com.rongc.client.freight.business.waybill.request.api.WaybillPayAliApi;
import com.rongc.client.freight.business.waybill.request.api.WaybillPayQianbaoApi;
import com.rongc.client.freight.business.waybill.request.api.WaybillStatusApi;
import com.rongc.client.freight.business.waybill.view.adapter.RecyclerWaybillAdapter;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.OnClick;

import static com.flyco.dialog.widget.NormalDialog.STYLE_TWO;
import static com.rongc.client.freight.business.supply.model.SupplyDic.StatusType.CANEL_CZ_STATUS;
import static com.rongc.client.freight.business.supply.model.SupplyDic.StatusType.COMPLATE_STATUS;
import static com.rongc.client.freight.business.supply.model.SupplyDic.StatusType.DTH_STATUS;
import static com.rongc.client.freight.business.supply.model.SupplyDic.StatusType.DZF_STATUS;
import static com.rongc.client.freight.business.supply.model.SupplyDic.StatusType.ROAD_STATUS;
import static com.rongc.client.freight.business.supply.model.SupplyDic.StatusType.SH_STATUS;
import static com.rongc.client.freight.business.supply.model.SupplyDic.StatusType.YAJIN_STATUS;

/**
 * Created by ccqx on 2017/7/21.
 */

public class WayBillActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.btn_submit)
    Button mBtnSubmit;
    @Bind(R.id.btn_pic)
    Button mBtnPic;
    @Bind(R.id.tv_status)
    TextView mTvStatus;
    @Bind(R.id.tv_total_2)
    TextView mTvTotal2;
    @Bind(R.id.tv_local)
    TextView mTvLocal;

    SupplyFragment supplyFragment;
    UserFragment userFragment;

    @Bind(R.id.recycler)
    RecyclerView mRecyclerView;
    RecyclerWaybillAdapter mLvAdapter;

    List<LogisticsBean> lsNews;

    int status ;
    String payWay = OrderDic.OrderType.Order_YUE;  // 我的钱包
    String payType = "1";  // 1：货主支付订单  3：车主支付押金
    String waybillId;
    WayBillDetailBean bean;

    PopupWindow popupWindowPay,popupWindowPayWay,popupWindowKeyword;
    NormalDialog caneldialog;
    NormalDialog cashdialog;
    NormalDialog payDialog;
    NormalDialog payResultDialog;
    NormalDialog defineDialog;
    NormalDialog complateDialog;
    List<String> pwdDatas = new ArrayList<>();
    int showcount ;
    Timer timer = new Timer();

    boolean isrun = false;  // 轮询标识
    boolean isComplate = false;  // 支付成功弹评论dialog

    Response.Listener<JSONObject> respAliPayListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                popupWindowPay.dismiss();
                Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(response);
                String orderParam = OrderInfoUtil2_0.buildOrderParam(params);

                String sign = OrderInfoUtil2_0.getSign(params);
                final String orderInfo = orderParam + "&" + sign;
                new DoAlipay().payAlipay(orderInfo, WayBillActivity.this, new DoAlipay.OnPayCompleteResyltListener() {
                    @Override
                    public void OnResult(String resultStr, String txt, boolean result) {
                        if(result){
                            //  支付成功
                            updateStatus();
                            ActivityUtils.toast("支付成功");
                        }else{
                            //  支付失败
                            ActivityUtils.toast("支付失败");
                        }
                    }
                });

            } else {
                UtilMethod.dismissProgressDialog(WayBillActivity.this);
            }
        }
    };


    Response.Listener<JSONObject> respQianbaoPayListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {

                if("1".equals(response.optString("status"))){
                    popupWindowPay.dismiss();
                    updateStatus();
                }else if("2".equals(response.optString("status"))){
                    popupWindowPayWay.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);;
                    ActivityUtils.toast(response.optString("code"));
                }else{
                    popupWindowPay.dismiss();
                    ActivityUtils.toast(response.optString("code"));
                }
            } else {
                UtilMethod.dismissProgressDialog(WayBillActivity.this);
            }
        }
    };

    Response.Listener<JSONObject> respwxPayListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                popupWindowPay.dismiss();
                Map<String,String> wxstr = null;
                wxstr = GsonHelper.jsonToMap(response.toString(),new TypeToken<Map<String, String>>(){});

                WxpayHelper.getInstent(WayBillActivity.this);
                WxpayHelper.pay(wxstr, WayBillActivity.this, new WxpayHelper.onWxpayPaylistener() {
                    @Override
                    public void payResult(String result) {

                    }
                });

            } else {
                UtilMethod.dismissProgressDialog(WayBillActivity.this);
            }
        }
    };

    void updateStatus(){
        int status = DTH_STATUS;
        if("1".equals(payType)){
            status = YAJIN_STATUS;
            showPayResultYJ();
        }else if("3".equals(payType)) {
            status = DTH_STATUS;
        }

        WaybillStatusApi pay = new WaybillStatusApi(new WaybillStatusApi.WaybillStatusParams(
                UniApplication.getInstance().getUserInfo().getUserId(),bean.getOrderid(), status+""),
                respWaybillPayListener, errorListener);
        RequestManager.getInstance().call(pay);
    }


    Response.Listener<NullResult> respWaybillPayListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            if (HandleCode.requestSuccess()) {
                popupWindowPay.dismiss();
                WaybillDetailApi p = new WaybillDetailApi(new WaybillDetailApi.WaybillDetailParams(
                        waybillId),
                        respWaybillListener, errorListener);
                RequestManager.getInstance().call(p);
            } else {
                UtilMethod.dismissProgressDialog(WayBillActivity.this);
            }
        }
    };

    @OnClick({R.id.tv_local,R.id.btn_submit,R.id.btn_pic})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_status:
                caneldialog.show();
                break;
            case R.id.tv_local:
                Bundle bundle = new Bundle();
                bundle.putString("lng", bean.getSjlng());
                bundle.putString("lat", bean.getSjlat());
                bundle.putString("title", bean.getSjnick());
                ActivityUtils.startActivity(this,MapGdActivity.class,bundle);
                break;
            case R.id.btn_pic:
                // 上传回单
                bundle = new Bundle();
                bundle.putString("waybillid",waybillId);
                ActivityUtils.startActivity(this,HuidanActivity.class,bundle);
                break;
            case R.id.btn_submit:
                if(status == DZF_STATUS){
                    if(popupWindowPay == null) return;
                    payType = "1";
                    //  支付弹框
                    popupWindowPay.getContentView().findViewById(R.id.tv_tip).setVisibility(View.VISIBLE);
                    popupWindowPay.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);;
                    WindowManager.LayoutParams lp = getWindow()
                            .getAttributes();
                    lp.alpha = 0.5f; // 0.0-1.0
                    getWindow().setAttributes(lp);
                }else if(status == YAJIN_STATUS && BaseDic.IdentityType.DRIVING_TYPE.equals(UniApplication.getInstance().getType()) ){
                    cashdialog.show();
                }else if(SupplyDic.getStatusBtn(status).equals("评论")){
                    //  评论
                    bundle = new Bundle();
                    bundle.putString("waybillid",waybillId);
                    ActivityUtils.startActivity(this,ReplyActivity.class,bundle);
                }else if(status == SH_STATUS){
                    defineDialog.show();
                }else {
                    UtilMethod.showProgressDialog(this);
                    int status = StringUtils.parseInt(v.getTag().toString());
//                    if(status == 10) status = 7;
                    WaybillStatusApi p = new WaybillStatusApi(new WaybillStatusApi.WaybillStatusParams(
                            UniApplication.getInstance().getUserInfo().getUserId(), bean.getOrderid(), status + ""),
                            respWaybillStatusListener, errorListener);
                    RequestManager.getInstance().call(p);
                }

                break;
            case R.id.btn_pay:
                //  调用支付 成功后修改状态
                switch (payWay){
                    case OrderDic.OrderType.Order_ALI:
                        WaybillPayAliApi pay = new WaybillPayAliApi(new WaybillPayAliApi.WaybillPayParams(
                                payType,
                                bean.getOrderid()),
                                respAliPayListener, errorListener);
                        RequestManager.getInstance().call(pay);
                        break;
                    case OrderDic.OrderType.Order_YUE:
                        pwdDatas.clear();

                        String cost = "";
                        if("1".equals(payType))
                            cost = bean.getCostByType();
                        else if("3".equals(payType))
                            cost = "100";
                        ((TextView)popupWindowKeyword.getContentView().findViewById(R.id.tv_total)).setText(
                                String.format(getResources().getString(R.string.tixian_popup_total),cost));

                        popupWindowKeyword.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
                        break;
                    case OrderDic.OrderType.Order_WX:
                        ActivityUtils.toast("暂不支持");
//                        WaybillPaywxApi wxpay = new WaybillPaywxApi(new WaybillPaywxApi.WaybillPaywxParams(
//                                payType,
//                                bean.getOrderid()),
//                                respwxPayListener, errorListener);
//                        RequestManager.getInstance().call(wxpay);

                        break;
                }

                break;
            case R.id.lyt_way:
                popupWindowPayWay.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);;
                break;
            case R.id.lyt_wx:
                ((TextView)popupWindowPay.getContentView().findViewById(R.id.tv_pay)).setText("微信");
                payWay = OrderDic.OrderType.Order_WX;
                popupWindowPayWay.dismiss();
                break;
            case R.id.lyt_zfb:
                ((TextView)popupWindowPay.getContentView().findViewById(R.id.tv_pay)).setText("支付宝");
                payWay = OrderDic.OrderType.Order_ALI;
                popupWindowPayWay.dismiss();
                break;
            case R.id.lyt_qb:
                ((TextView)popupWindowPay.getContentView().findViewById(R.id.tv_pay)).setText("我的钱包");
                payWay = OrderDic.OrderType.Order_YUE;
                popupWindowPayWay.dismiss();
                break;
            case R.id.iv_close_way:
                popupWindowPayWay.dismiss();
                break;
            case R.id.iv_close:
                popupWindowPay.dismiss();
                popupWindowKeyword.dismiss();
                break;
            case R.id.tv_forget:
                ActivityUtils.startActivity(this,PasswordPayActivity.class);
                break;
        }
    }

    Response.Listener<WayBillDetailBean> respWaybillListener = new Response.Listener<WayBillDetailBean>() {

        @Override
        public void onResponse(WayBillDetailBean response) {
            if (HandleCode.requestSuccess()) {
                bean = response;
                status = StringUtils.parseInt(bean.getWaystate());
                lsNews.clear();
                if(response!=null&&response.getWuliulist()!=null){
                    lsNews.addAll(response.getWuliulist());
                }
                mLvAdapter.notifyDataSetChanged();
                initData();

                String price = "";
                if(bean != null) price = bean.getCostByType();
                ((TextView)popupWindowPay.getContentView().findViewById(R.id.tv_total)).setText("￥"+price);
                complateDialog.content("运费"+price + "已结算给货运司机\n请对本次服务的司机做个评价。");
                if(isComplate)
                if(status == COMPLATE_STATUS){
                    isComplate = false;
                    complateDialog.show();
                }
            } else {
                UtilMethod.dismissProgressDialog(WayBillActivity.this);
            }
        }
    };

    Response.Listener<NullResult> respWaybillStatusListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            UtilMethod.dismissProgressDialog(WayBillActivity.this);
            if (HandleCode.requestSuccess()) {
                switch (status){
                    case DTH_STATUS:
                        showTH();
                        break;
                    case ROAD_STATUS:
                        showHD();
                        break;
                }
                WaybillDetailApi p = new WaybillDetailApi(new WaybillDetailApi.WaybillDetailParams(
                        waybillId),
                        respWaybillListener, errorListener);
                RequestManager.getInstance().call(p);

            } else {
            }
        }
    };

    Response.Listener<NullResult> respCancelListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            if (HandleCode.requestSuccess()) {
                caneldialog.cancel();
                finish();
            } else {
                UtilMethod.dismissProgressDialog(WayBillActivity.this);
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            UtilMethod.dismissProgressDialog(WayBillActivity.this);
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    @Override
    public void initData() {
        if(bean != null) {

            userFragment.setData(bean.getSjimg(),bean.getSjnick(),
                    bean.getXiadanNumber(),bean.getJiedanNumber(),bean.getScore(),bean.getSjmobile());

            supplyFragment.setData(bean);
            supplyFragment.setListener(this);

            mTvTotal2.setText(Html.fromHtml(String.format(
                    getResources().getString(R.string.waybill_total_lable),bean.getCostByType())));

            switch (UniApplication.getInstance().getType()) {
                case BaseDic.IdentityType.OWNER_TYPE:
                    initToolbar(SupplyDic.getStatusTitle(status));
                    mTvStatus.setText(SupplyDic.getStatusStr(status));
                    String btnStr = SupplyDic.getStatusBtn(status);

                    if (StringUtils.isEmpty(btnStr)) mBtnSubmit.setVisibility(View.GONE);
                    else {
                        mBtnSubmit.setVisibility(View.VISIBLE);
                        mBtnSubmit.setText(btnStr);
                        mBtnSubmit.setTag(SupplyDic.getNextStatus(status));
                    }
                    break;
                case BaseDic.IdentityType.DRIVING_TYPE:
                    mTvLocal.setVisibility(View.GONE);
                    initToolbar(ConveyanceDic.getStatusTitle(status));
                    mTvStatus.setText(ConveyanceDic.getStatusStr(status));
                    btnStr = ConveyanceDic.getStatusBtn(status);
                    if (StringUtils.isEmpty(btnStr)) mBtnSubmit.setVisibility(View.GONE);
                    else {
                        mBtnSubmit.setVisibility(View.VISIBLE);
                        mBtnSubmit.setText(btnStr);
                        mBtnSubmit.setTag(ConveyanceDic.getNextStatus(status));
                    }

                    if(status == ROAD_STATUS || status == SH_STATUS || status == COMPLATE_STATUS) {
                        if("1".equals(bean.getIs_huidan()))
                            mBtnPic.setVisibility(View.GONE);
                        else
                            mBtnPic.setVisibility(View.VISIBLE);
                    }
                    break;
            }


            if(status == COMPLATE_STATUS && "1".equals(bean.getIs_comment())){
                mBtnSubmit.setVisibility(View.GONE);
            }

            if(status == DZF_STATUS){
                mTvTotal2.setVisibility(View.GONE);
                if(showcount == 0)
                    payDialog.show();
            }else{
                mTvTotal2.setVisibility(View.VISIBLE);
            }

            if(status == YAJIN_STATUS && BaseDic.IdentityType.DRIVING_TYPE.equals(UniApplication.getInstance().getType()) ) {
                if(showcount == 0)
                    cashdialog.show();
            }
            showcount++;
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        WaybillDetailApi p = new WaybillDetailApi(new WaybillDetailApi.WaybillDetailParams(
                waybillId),
                respWaybillListener, errorListener);
        RequestManager.getInstance().call(p);
    }

    @Override
    public void initParams() {
        status = getIntent().getIntExtra("status", CANEL_CZ_STATUS);
        waybillId = getIntent().getStringExtra("data");
    }

    @Override
    public void initView() {
        lsNews = new ArrayList<>();

        mLvAdapter = new RecyclerWaybillAdapter(this, lsNews);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(mLvAdapter);
        supplyFragment = (SupplyFragment) getSupportFragmentManager().findFragmentByTag("supply");
        userFragment = (UserFragment) getSupportFragmentManager().findFragmentByTag("user");

        findViewById(R.id.tv_jia).setVisibility(View.GONE);

        makePayWayPopup();
        makeCanelDialog();
        makeCashDialog();
        makePayDialog();
        makePayPopup();
        makeKeywordPopup();
        makeResultDialog();
        makeDefineDialog();
        makeComplateDialog();

        if(!isrun)
            timer.schedule(task,500,5000);
    }

    TimerTask task= new TimerTask() {
        @Override
        public void run() {
            isrun = true;
            WaybillDetailApi p = new WaybillDetailApi(new WaybillDetailApi.WaybillDetailParams(
                    waybillId),
                    respWaybillListener, errorListener);
            RequestManager.getInstance().call(p);
        }
    };


    public void makePayPopup(){
        String price = "";
        if(bean != null) price = bean.getCostByType();
        popupWindowPay = SupplyPopup.makePopupPayOption(this,price,this);
        popupWindowPay.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow()
                        .getAttributes();
                lp.alpha = 1f; // 0.0-1.0
                getWindow().setAttributes(lp);
            }
        });
    }

    public void makePayWayPopup(){
        popupWindowPayWay = SupplyPopup.makePopupPayWayOption(this,this);
    }

    void makeCanelDialog(){
        caneldialog = new NormalDialog(this);
        caneldialog.content("您找到车子了吗，要不再等等？")
                .style(STYLE_TWO)
                .btnText(new String[]{"取消","确定"})
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        caneldialog.cancel();
                    }
                },new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        //  取消绑定
                        WaybillCancelApi p = new WaybillCancelApi(new WaybillCancelApi.WaybillCancelParams(
                                waybillId),
                                respCancelListener, errorListener);

                        RequestManager.getInstance().call(p);
                    }
                });
    }

    void makeCashDialog(){
        if(cashdialog != null) return;
        cashdialog = new NormalDialog(this);
        cashdialog.content("货主已经支付运费，为了保证货物安全，请您支付100块押金。货主确认货到后，押金将返还到您的钱包。")
                .style(STYLE_TWO)
                .btnText(new String[]{"取消","确定"})
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        cashdialog.cancel();
                    }
                },new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        cashdialog.cancel();
                        if(popupWindowPay == null) return;
                        //  支付押金
                        payType = "3";
                        ((TextView)popupWindowPay.getContentView().findViewById(R.id.tv_total)).setText("￥100");
                        popupWindowPay.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);;
                        WindowManager.LayoutParams lp = getWindow()
                                .getAttributes();
                        lp.alpha = 0.5f; // 0.0-1.0
                        getWindow().setAttributes(lp);


                    }
                });
    }

    void makePayDialog(){
        payDialog = new NormalDialog(this);
        payDialog.isTitleShow(false)
                .content("线上支付送货物保险，为了保障您的货物安全，请线上支付。")
                .style(STYLE_TWO)
                .btnText(new String[]{"取消","支付"})
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        payDialog.cancel();
                    }
                },new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        findViewById(R.id.btn_submit).performClick();
                        payDialog.cancel();
                    }
                });
    }

    void makeResultDialog(){
        payResultDialog = new NormalDialog(this);
        payResultDialog.title("成功")
                .content("优质的信誉师傅，上面提货要准时哦，装货完成，记得点击【确认提货】哟")
                .style(STYLE_TWO)
                .btnText(new String[]{"好滴，记住了"})
                .cornerRadius(8)
                .dimEnabled(true)
                .setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                payResultDialog.cancel();
            }
        });
    }

    void makeDefineDialog(){
        defineDialog = new NormalDialog(this);
        defineDialog.title("确认收货")
                .content("确认收货后，平台将运费结算给货运司机")
                .btnText(new String[]{"取消","确认收货"})
                .style(STYLE_TWO)
                .dimEnabled(true)
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        defineDialog.cancel();
                    }
                }, new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        isComplate = true;

                        WaybillStatusApi p = new WaybillStatusApi(new WaybillStatusApi.WaybillStatusParams(
                                UniApplication.getInstance().getUserInfo().getUserId(), bean.getOrderid(), COMPLATE_STATUS + ""),
                                respWaybillStatusListener, errorListener);
                        RequestManager.getInstance().call(p);
                        defineDialog.cancel();
                    }
                });
    }

    void makeComplateDialog(){
        complateDialog = new NormalDialog(this);
        complateDialog.title("运输完成")
                .content("运费已结算给货运司机\n请对本次服务的司机做个评价。")
                .btnText(new String[]{"有空再说","评价"})
                .style(STYLE_TWO)
                .dimEnabled(true)
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        complateDialog.cancel();
                    }
                }, new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {

                        Bundle bundle = new Bundle();
                        bundle.putString("waybillid",waybillId);
                        ActivityUtils.startActivity(WayBillActivity.this,ReplyActivity.class,bundle);
                        complateDialog.cancel();
                    }
                });
    }

    void showPayResultYJ(){
        payResultDialog.title("支付成功")
                .content("优质的信誉师傅，上面提货要准时哦，装货完成，记得点击【确认提货】哟")
                .btnText(new String[]{"好滴，记住了"});
        payResultDialog.show();
    }

    void showTH(){
        payResultDialog.title("起程运输咯")
                .content("运输交给师傅您，我们很放心\n速拉回程车将竭诚为您服务！")
                .btnText(new String[]{"好的"});
        payResultDialog.show();
    }

    void showHD(){
        payResultDialog.title("谢谢师傅辛苦了")
                .content("稍后货主确认后，运费打到您的钱包里，日常运输多注意身体！")
                .btnText(new String[]{"好的"});
        payResultDialog.show();
    }


    public void makeKeywordPopup(){
        final RecyclerKeyboardAdapter mAdapter = new RecyclerKeyboardAdapter(this,pwdDatas);
        popupWindowKeyword = MinePopup.makePopupPayTxOption(this, mAdapter, this, new KeyboardUtil.KeyBoardListener() {
            @Override
            public void onChange() {
                if(mAdapter.getDatas().size() <= 6)
                    mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onComplete() {
                WaybillPayQianbaoApi qianbaopay = new WaybillPayQianbaoApi(new WaybillPayQianbaoApi.WaybillPayQianbaoParams(
                        payType,
                        bean.getOrderid(),mAdapter.getData()),
                        respQianbaoPayListener, errorListener);
                RequestManager.getInstance().call(qianbaopay);
                popupWindowKeyword.dismiss();
//                }else
//                    ActivityUtils.toast("密码错误");
            }
        });
    }

    @Override
    public void onBackPressed() {
        ActivityUtils.startActivity(this, HostActivity.class);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
                ActivityUtils.startActivity(this, HostActivity.class);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_waybill;
    }
}
